<template>
  <div>
    <el-card style="width: 40%;">
      <el-form label-width="80px" style="padding-right: 30px" :rules="rules" :model="user" class="demo-form-inline">

        <div style="text-align: center">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/file/upload"
              :show-file-list="false"
              :headers="{token: user.token}"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>

        <el-form-item style="margin-top: 30px;" label="用户名">
          <el-input v-model="user.username" disabled></el-input>
        </el-form-item>

        <el-form-item prop="name" style="margin-left: 2px" label="姓名">
          <el-input v-model="user.name"></el-input>
        </el-form-item>

        <el-form-item prop="phone" style=" margin-left: 2px" label="电话">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>

        <el-form-item prop="email" style=" margin-left: 2px" label="邮箱">
          <el-input v-model="user.email"></el-input>
        </el-form-item>

        <el-form-item prop="address" style=" margin-left: 2px" label="地址">
          <el-input  type="textarea" v-model="user.address"></el-input>
        </el-form-item>

        <div style="text-align: center;margin-right: 70px">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即修改</el-button>
          </el-form-item>
        </div>

      </el-form>
    </el-card>
  </div>
</template>

<script>
import reuqest from "@/utils/request";

export default {
  name: "Test",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user11') || '{}'),
      users: {
        ...JSON.parse(localStorage.getItem('user11') || '{}'),
        phone: '',
        email: '',
        address: '',
        name: '',
      },

      // rules: {
      //   phone: [
      //     {required: true, message: '请输入手机号', trigger: 'blur'},
      //     {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
      //   ],
      //   email: [
      //     {required: true, message: '请输入邮箱地址', trigger: 'blur'},
      //     {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
      //   ],
      //   name: [
      //     {required: true, message: '请输入姓名', trigger: 'blur'},
      //     {min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur'}
      //   ],
      //   address: [
      //     {required: true, message: '请输入地址', trigger: 'blur'}
      //   ]
      // }
    };
  },
  methods: {

    //确认是否确认提交修改
    onSubmit() {
      this.$confirm('确定用户信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //触发更新
        reuqest.post('/user/update', this.user).then(res => {
          if (res.code === 200) {
            this.$message.success('修改成功')
            //  更新浏览器缓存里面的用户信息
            localStorage.setItem('user11', JSON.stringify(this.user))
            //  浏览器页面自动更新
            setTimeout(() => {
              this.$router.go(0);
            }, 1000);
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消修改'});
        setTimeout(() => {
          this.$router.go(0);
        }, 1000);
      })
    },
    handleAvatarSuccess(res, file, fileList) {
      this.user.avatar = res.data
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>